import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
import "qrc:/component/ZGlobal.js" as Global
import "../component" as C

C.ZModelRect {
    id: rootLoginPage
    width: 780
    height: 680
    anchors.centerIn: parent
    color: "transparent"

    property string errorMessage: ""
    property string errorType: "" // "account" 或 "password" 或 ""

    // 清空输入与错误状态（在打开弹窗前调用）
    function resetForm() {
        rootLoginPage.errorMessage = ""
        rootLoginPage.errorType = ""
        name.text = ""
        password.text = ""
    }


    Component.onCompleted: {
        modelMask = Global.createModel(root);
        modelParent=rootLoginPage.parent;
        rootLoginPage.parent=modelMask;
    }

    Image {
        anchors.fill: parent
        source: "qrc:/assets/login_bg@2x.png"
        fillMode: Image.PreserveAspectCrop
        layer.enabled: true
    }

    ColumnLayout {
        x:170
        y:100
        Layout.alignment: Qt.AlignHCenter
        spacing: -50

        Rectangle {
            width: loginTitleLabel.width
            height: 12
            color: "#2090CD"
            Layout.alignment: Qt.AlignHCenter
        }

        Label {
            id: loginTitleLabel
            text: "一次性磁力搅拌系统登录"
            font.pixelSize: 40
            font.bold: true
            color: "#333"
            Layout.alignment: Qt.AlignHCenter
        }
    }

    Image{
        x:80
        y:190
        width: 623
        height: 93
        source: "qrc:/assets/login_user@2x.png"
        fillMode: Image.PreserveAspectCrop
        TextField {
            id: name
            text: "admin"
            Layout.fillWidth: true
            Layout.fillHeight: true
            Layout.alignment: Qt.AlignVCenter
            anchors.fill: parent
            leftPadding: 126
            placeholderText: "请输入登录名"
            placeholderTextColor: "#D1D3D8"
            font.pixelSize: 32
            rightPadding: 20
            color: "black"

            verticalAlignment: Text.AlignVCenter
            background: null
        }
    }

    Image{
        x:80
        y:320
        width: 623
        height: 93
        source: "qrc:/assets/login_pwd@2x.png"
        fillMode: Image.PreserveAspectCrop

        TextField {
            id: password
            text: "123"
            Layout.fillWidth: true
            Layout.fillHeight: true
            Layout.alignment: Qt.AlignVCenter
            anchors.fill: parent
            leftPadding: 126
            font.pixelSize: 32
            placeholderText: "请输入密码"
            placeholderTextColor: "#D1D3D8"
            rightPadding: 20
            color: "black"
            echoMode: TextInput.Password
            verticalAlignment: Text.AlignVCenter
            background: null
        }
    }

    // 错误信息提示
    Text {
        id: errorMsg
        x:80
        y:430
        text: rootLoginPage.errorMessage
        visible: errorMsg.text.length > 0
        color: "#E53935"
        font.pixelSize: 18
        Layout.alignment: Qt.AlignHCenter
    }

    Rectangle {
        id: loginBtn
        x:76
        y:484
        width: 644
        height: 114
        radius: 10
        color: "transparent"
        Layout.alignment: Qt.AlignHCenter
        property bool hovered: false
        property bool pressed: false
        scale: pressed ? 0.98 : 1

        Image{
            anchors.fill: parent
            source: "qrc:/assets/login_button@2x.png"
            fillMode: Image.PreserveAspectCrop
            layer.enabled: true

            Text {
                text: "登录"
                color: "white"
                font.pixelSize: 36
                font.bold: true
                anchors.centerIn: parent
                anchors.verticalCenterOffset: -10
            }

            MouseArea {
                anchors.fill: parent
                hoverEnabled: true
                onEntered: loginBtn.hovered = true
                onExited: loginBtn.hovered = false
                onPressed: loginBtn.pressed = true
                onReleased: loginBtn.pressed = false
                onClicked: {
                    console.log("登录按钮被点击", name.text, password.text)
                    var loginResp = userInfo.login(name.text, password.text)

                    // 检查是否是许可被禁用的用户
                    if (loginResp && loginResp.status === "0") {
                        rootLoginPage.errorMessage = "账号已被禁用"
                        rootLoginPage.errorType = "disabled"
                        console.log("登录失败：用户许可被禁用")
                        return
                    }

                    console.log("status:", loginResp.status);
                    if (loginResp && loginResp.name && loginResp.status === "1") {
                        rootLoginPage.errorMessage = ""
                        rootLoginPage.errorType = ""
                        rootLoginPage.visible = false
                        console.log("登录成功：", loginResp.name)
                        var isFirst = userInfo.isFirstLogin();
                        if (isFirst === true){
                            modifyPasswordPage.item.resetForm()
                            modifyPasswordPage.item.visible = true
                        }

                        updateMenuByRoleId(parseInt(loginResp.permission))
                    } else {
                        var exists = false
                        for (var i = 0; i < userInfo.count(); i++) {
                            var r = userInfo.get(i)
                            if (r && r.name === name.text) { exists = true; break }
                        }
                        if (!exists) {
                            rootLoginPage.errorMessage = "账号错误"
                            rootLoginPage.errorType = "account"
                            console.log("登录失败：账号不存在")
                        } else {
                            rootLoginPage.errorMessage = "密码错误"
                            rootLoginPage.errorType = "password"
                            console.log("登录失败：密码错误")
                        }
                    }
                }
            }
        }
    }
}
